package com.cast.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.cast.Utils.DateUtils;
import com.cast.constant.MessageConstant;
import com.cast.dao.MemberDao;
import com.cast.dao.OrderDao;
import com.cast.dao.OrderSettingDao;
import com.cast.entity.Result;
import com.cast.pojo.Member;
import com.cast.pojo.Order;
import com.cast.pojo.OrderSetting;
import com.cast.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @项目名称 health
 * @包名 com.cast.service.impl
 * @类名 OrderServiceImpl
 * @作者 lx
 * @时间 2020/3/7 23:22
 * @描述
 */
@Service(interfaceClass = OrderService.class)
@Transactional
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDao orderDao;
    @Autowired
    private OrderSettingDao orderSettingDao;
    @Autowired
    private MemberDao memberDao;

    @Override
    public Result order(Map map) throws Exception{
        // 检查用户所选择的预约日期是否已经提前进行了预约设置，如果没有，则无法进行预约
        String orderDate = (String) map.get("orderDate");
        Date date = DateUtils.parseString2Date(orderDate);
        OrderSetting orderSetting = orderSettingDao.findOrderSetting(date);
        if (orderSetting == null){
            return new Result(false, MessageConstant.SELECTED_DATE_CANNOT_ORDER);
        }
        // 检查用户所选择的预约日期是否已经约满，如果已经约满，则无法预约
        int number = orderSetting.getNumber(); // 可预约人数
        int reservations = orderSetting.getReservations(); // 已预约人数
        if (reservations >= number){
            // 预约已满，不能预约
            return new Result(false, MessageConstant.ORDER_FULL);
        }
        // 检查用户是否重复预约，如果重复预约，则无法再次预约,根据手机号判断
        String telephone = (String) map.get("telephone"); // 获取用户页面输入的手机号
        Member member = memberDao.findByTelephone(telephone);
        // 防止重复预约
        if (member != null){
            Integer memberId = member.getId();
            int setmealId = Integer.parseInt((String) map.get("setmealId"));
            Order order = new Order(memberId, date, null, null, setmealId);
            List<Order> list = orderDao.findByCondition(order);
            if (list != null && list.size() > 0){
                // 已经完成了预约，不能重复预约
                return new Result(false, MessageConstant.HAS_ORDERED);
            }
        }else {
            // 检查当前用户是否为会员，是会员则直接预约，不是完成注册在预约
            //当前用户不是会员，需要添加到会员表
            member = new Member();
            member.setName((String) map.get("name"));
            member.setPhoneNumber(telephone);
            member.setIdCard((String) map.get("idCard"));
            member.setSex((String) map.get("sex"));
            member.setRegTime(new Date());
            // 自动完成会员注册
            memberDao.add(member);
        }
        // 预约成功，更新当日已预约人数
        Order order = new Order();
        // 设置会员id
        order.setMemberId(member.getId());
        // 设置预约日期
        order.setOrderDate(date);
        // 设置预约类型
        order.setOrderType((String) map.get("orderType"));
        // 设置到诊状态
        order.setOrderStatus(Order.ORDERSTATUS_NO);
        // 设置套餐id
        order.setSetmealId(Integer.parseInt((String)map.get("setmealId")));
        orderDao.add(order);
        // 设置已预约人数 + 1
        orderSetting.setReservations(orderSetting.getReservations() + 1);
        orderSettingDao.addOrderSetting(orderSetting);
        return new Result(true,MessageConstant.ORDER_SUCCESS,order.getId());
    }
}
